.TH std::move_iterator::operator[] 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::move_iterator::operator[] \- std::move_iterator::operator[]

.SH Synopsis
   /* unspecified */ operator[]( difference_type n ) const;            \fI(since C++11)\fP
                                                                       \fI(until C++17)\fP
   constexpr /* unspecified */ operator[]( difference_type n ) const;  \fI(since C++17)\fP
                                                                       \fI(until C++20)\fP
   constexpr reference operator[]( difference_type n ) const;          \fI(since C++20)\fP

   Returns a reference to the element at specified relative location.

.SH Parameters

   n - position relative to current location

.SH Return value

   An rvalue reference to the element at relative location, that is,
   std::move(base()[n])
   \fI(until C++20)\fP
   ranges::iter_move(base() + n)
   \fI(since C++20)\fP.

.SH Notes

   The return type is unspecified because the return type of the
   underlying iterator's operator[] is also unspecified (see              \fI(until C++20)\fP
   LegacyRandomAccessIterator).

.SH Example


// Run this code

 #include <cstddef>
 #include <iomanip>
 #include <iostream>
 #include <iterator>
 #include <list>
 #include <string>
 #include <vector>

 void print(auto rem, auto const& v)
 {
     for (std::cout << rem; auto const& e : v)
         std::cout << std::quoted(e) << ' ';
     std::cout << '\\n';
 }

 int main()
 {
     std::vector<std::string> p{"alpha", "beta", "gamma", "delta"}, q;
     print("1) p: ", p);

     std::move_iterator it{p.begin()};

     for (std::size_t t{}; t != p.size(); ++t)
         q.emplace_back(it[t]);

     print("2) p: ", p);
     print("3) q: ", q);

     std::list l{1, 2, 3};
     std::move_iterator it2{l.begin()};
 //  it2[1] = 13; // compilation error: the underlying iterator
                  // does not model the random access iterator
 //  *it2 = 999;  // compilation error: using rvalue as lvalue
 }

.SH Possible output:

 1) p: "alpha" "beta" "gamma" "delta"
 2) p: "" "" "" ""
 3) q: "alpha" "beta" "gamma" "delta"

.SH See also

   operator*
   operator->                   accesses the pointed-to element
   \fI(C++11)\fP                      \fI(public member function)\fP
   \fI(C++11)\fP(deprecated in C++20)
